package com.yixing.tech.common.tcp.serialize;

import com.yixing.tech.common.utils.DecimalConvertUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class HexByteDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int len = in.readableBytes();
        in.markReaderIndex();

        log.info("可读取大小: {}", len);
        ByteBuf byteBuf = in.readBytes(len);
        byte[] bytes = new byte[len];
        byteBuf.readBytes(bytes);
        String res = DecimalConvertUtil.BinaryToHexString(bytes);
        out.add(res);
    }
}
